<template>
  <div class="result-container">
    <div class="title-wrap">
      <h2 class="title">{{ $route.query.q }}</h2>
      <span class="sub-title">找到{{ count }}个结果</span>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr
              class="el-table__row"
              v-for="(item, i) in songList"
              :key="i"
              @dblclick="playMusic(item.id)"
            >
              <td>1</td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{ item.name }}</span>
                    <span v-if="item.mvid != 0" class="iconfont icon-mv"></span>
                  </div>
                  <span v-if="item.alias.length != 0">{{ item.alias[0] }}</span>
                </div>
              </td>
              <td>{{ item.artists[0].name }}</td>
              <td>{{ item.album.name }}</td>
              <td>{{ item.duration }}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="lists">
        <div class="items">
          <div
            class="item"
            v-for="(item, i) in playLists"
            :key="i"
            @click="toPlaylist(item.id)"
          >
            <div class="img-wrap">
              <div class="num-wrap">
                播放量:
                <span class="num">{{ item.playCount }}</span>
              </div>
              <img :src="item.coverImgUrl" alt />
              <span class="iconfont icon-play"></span>
            </div>
            <p class="name">{{ item.name }}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div
            class="item"
            v-for="(item, i) in mv"
            :key="i"
            @click="toMV(item.id)"
          >
            <div class="img-wrap">
              <img :src="item.cover" alt />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">{{ item.playCount }}</div>
              </div>
              <span class="time">{{ item.duration }}</span>
            </div>
            <div class="info-wrap">
              <div class="name">{{ item.name }}</div>
              <div class="singer">{{ item.artistName }}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
// import axios from "axios";
import { getResult, playMusic } from "../network/result";
import { mapMutations, mapGetters } from "vuex";
export default {
  name: "result",
  data() {
    return {
      activeIndex: "songs",
      songList: [],
      // 搜索结果的总数
      count: 0,
      // 保存歌单字段
      playLists: [],
      // 保存mv的字段
      mv: [],
      q: this.$route.query.q,
    };
  },
  computed: {
    ...mapGetters(["isPlay"]),
  },
  watch: {
    activeIndex() {
      let type = 1;
      let limit = 10;
      switch (this.activeIndex) {
        case "songs":
          type = 1;
          limit: 10;
          break;
        case "lists":
          type = 1000;
          limit: 10;
          break;
        case "mv":
          type = 1004;
          limit: 8;
          break;
      }

      getResult(this.$route.query.q, type, limit).then((res) => {
        if (type == 1) {
          this.songList = res.data.result.songs;
          // 处理歌曲时间
          for (let i = 0; i < this.songList.length; i++) {
            let min = parseInt(this.songList[i].duration / 1000 / 60);
            let sec = parseInt((this.songList[i].duration / 1000) % 60);
            if (min < 10) {
              min = "0" + min;
            }
            if (sec < 10) {
              sec = "0" + sec;
            }
            this.songList[i].duration = min + ":" + sec;
          }
          this.count = res.data.result.songCount;
        } else if (type == 1000) {
          // 歌单逻辑
          this.playLists = res.data.result.playlists;
          // 总数
          this.count = res.data.result.playlistCount;
          // 次数处理
          for (let i = 0; i < this.playLists.length; i++) {
            if (this.playLists[i].playCount > 100000) {
              this.playLists[i].playCount =
                parseInt(this.playLists[i].playCount / 10000) + "万";
            }
          }
        } else {
          this.mv = res.data.result.mvs;
          // 总数
          this.count = res.data.result.mvCount;
          // 处理数据
          for (let i = 0; i < this.mv.length; i++) {
            //  时间
            let min = parseInt(this.mv[i].duration / 1000 / 60);
            let sec = parseInt((this.mv[i].duration / 1000) % 60);
            if (min < 10) {
              min = "0" + min;
            }
            if (sec < 10) {
              sec = "0" + sec;
            }
            this.mv[i].duration = min + ":" + sec;

            // 播放次数
            if (this.mv[i].playCount > 100000) {
              this.mv[i].playCount =
                parseInt(this.mv[i].playCount / 10000) + "万";
            }
          }
        }
      });
    },
    "$route.query.q": function(to, from) {
      this.q = to;
      getResult(this.$route.query.q, 1, 10).then((res) => {
        this.songList = res.data.result.songs;
        // 处理歌曲时间
        for (let i = 0; i < this.songList.length; i++) {
          let min = parseInt(this.songList[i].duration / 1000 / 60);
          let sec = parseInt((this.songList[i].duration / 1000) % 60);
          if (min < 10) {
            min = "0" + min;
          }
          if (sec < 10) {
            sec = "0" + sec;
          }
          this.songList[i].duration = min + ":" + sec;
        }
        this.count = res.data.result.songCount;
      });
    },
  },
  methods: {
    ...mapMutations(["setUrl", "setIsPlay"]),
    playMusic(id) {
      // 播放歌曲
      // axios({
      //   url: "https://autumnfish.cn/song/url",
      //   method: "get",
      //   params: { id },
      // }).then((res) => {
      //   // console.log(res);
      //   let url = res.data.data[0].url;
      //   // console.log(url);
      //   // console.log(this.$parent.musicUrl);
      //   this.$parent.musicUrl = url;
      // });
      playMusic(id).then((res) => {
        let url = res.data.data[0].url;
          console.log(res);
        //   // console.log(this.$parent.musicUrl);
        // this.$parent.musicUrl = url;
        // this.$store.commit('setUrl',url)
        this.setUrl(url);
        console.log(this.isPlay)
        if (this.isPlay) {
          this.setIsPlay(false);
        } else {
          this.setIsPlay(true);
        }
      });
    },
    toPlaylist(id) {
      this.$router.push(`/playlist?q=${id}`);
    },
    toMV(id) {
      this.$router.push(`/mv?q=${id}`);
    },
  },
  created() {
    getResult(this.$route.query.q, 1, 10).then((res) => {
      if(!res.data.result.songs.length){
        this.notify('系统暂无符合条件的歌曲','warning')
      }else{
        this.songList = res.data.result.songs;
        //  console.log(this.songList)
      }
      // this.songList = res.data.result.songs;
     
      // 处理歌曲时间
      for (let i = 0; i < this.songList.length; i++) {
        let min = parseInt(this.songList[i].duration / 1000 / 60);
        let sec = parseInt((this.songList[i].duration / 1000) % 60);
        if (min < 10) {
          min = "0" + min;
        }
        if (sec < 10) {
          sec = "0" + sec;
        }
        this.songList[i].duration = min + ":" + sec;
      }
      this.count = res.data.result.songCount;
    });
  },
};
</script>

<style></style>
